<?php
namespace Admin\Model;
use Think\Model;
//用户模型
class RechargeModel extends Model{
    //快速充值
    public function recharge($goods_type,$user_type,$userid,$num){
        if($user_type == 1)  //$type == 1 则为代理充值
        {
            $where = array('agentid'=>$userid,'status'=>0);
            if(UID != 1){
                $where['p1'] = UID;
            }
            //查询代理号是否正确
            $member = M('Member')->where($where)->find();
            if(!$member){$this->error='该代理不存在或者被禁用';return false;}
            if($num<=1){$this->error='充值数量不能小于等于1';return false;}
            //充值物品类型
            $my = M('Member')->find(UID);
            if($goods_type == 1)
            {
                if(UID !=1){
                    if ($my['jewels'] < $num) {
                        $this->error = '您的房卡不足，请先补货';
                        return false;
                    }
                }
                //进行充值
                $res =M('Member') -> where(array('agentid'=>$userid))->save(array('jewels'=>array('exp','[jewels]+'.$num)));
                $pes =M('Member') -> where(array('id'=>UID))->save(array('jewels'=>array('exp','[jewels]-'.$num)));
                //分佣
                $p = $this->commission(1,UID,$num);
            }
            elseif($goods_type == 2)
            {
                if(UID !=1){
                    if ($my['money'] < $num) {
                        $this->error = '您的金币不足，请先补货';
                        return false;
                    }
                }
                //进行充值
                $res =M('Member') -> where(array('agentid'=>$userid))->save(array('money'=>array('exp','[money]+'.$num)));
                $pes =M('Member') -> where(array('id'=>UID))->save(array('money'=>array('exp','[money]-'.$num)));
                //分佣
                $p = $this->commission(2,UID,$num);
            }
            //记录
            $data=array('type'=>1,'num'=>$num,'mid'=>$my['agentid'],'uid'=>$member['agentid'],'muser'=>$my['username'],'tuser'=>$member['username'],'p1'=>$p['p1'],'p2'=>$p['p2'],'p3'=>$p['p3']);
            record_log($data);
            return true;
        }
        elseif($user_type == 2)  //为2则为玩家充值
        {   //查询玩家号是否正确
            $member = M('')->where(array('userID'=>$userid,'status'=>0))->find();
            if(!$member){$this->error='该玩家不存在或者被禁用';return false;}
        }
    }



    /*分佣
     * 参数
     * $goods_type  物品类型
     * $member_id   消费代理ID
     * $num         消费数额
     * */
    public function commission($goods_type,$member_id,$num){
        //获取分佣比例
        $level1 = M('commissionlevel')->select();
        $level = array();
        foreach($level1 as $k => $v){
            $level[$level1[$k]['key']] = $level1[$k]['value'];
        }
        $p1_level = $level['p1']/100;
        $p3_level = $level['p2']/100;
        $p2_level = $level['p3']/100;
        //查询代理信息
        $member = M('Member')->where(array('id'=>$member_id))->find();
        //向三级代理进行分销
        //房卡
        $p = array();
        if($goods_type == 1){
            if ($member['p1'] != 0) {
                M('Member')->where(array('id' => $member['p1']))->save(array('commission_jewels' => array('exp', '[commission_jewels]+' .($num*$p1_level))));
                $p['p1'] = $num*$p1_level;
            }else{
                $p['p1'] = 0.00;
            }
            if ($member['p2'] != 0) {
                M('Member')->where(array('id' => $member['p2']))->save(array('commission_jewels' => array('exp', '[commission_jewels]+' .($num*$p2_level))));
                $p['p2'] = $num*$p2_level;
            }else{
                $p['p2'] = 0.00;
            }
            if ($member['p3'] != 0) {
                M('Member')->where(array('id' => $member['p3']))->save(array('commission_jewels' => array('exp', '[commission_jewels]+' .($num*$p3_level))));
                $p['p3'] = $num*$p3_level;
            }else{
                $p['p3'] = 0.00;
            }
        }elseif($goods_type == 2){
            if ($member['p1'] != 0) {
                M('Member')->where(array('id' => $member['p1']))->save(array('commission_money' => array('exp', '[commission_money]+' .($num*$p1_level))));
                $p['p1'] = $num*$p1_level;
            }else{
                $p['p1'] = 0.00;
            }
            if ($member['p2'] != 0) {
                M('Member')->where(array('id' => $member['p2']))->save(array('commission_money' => array('exp', '[commission_money]+' .($num*$p2_level))));
                $p['p2'] = $num*$p1_level;
            }else{
                $p['p2'] = 0.00;
            }
            if ($member['p3'] != 0) {
                M('Member')->where(array('id' => $member['p3']))->save(array('commission_money' => array('exp', '[commission_money]+' .($num*$p3_level))));
                $p['p3'] = $num*$p1_level;
            }else{
                $p['p3'] = 0.00;
            }
        }

        return $p;

    }
}



?>